package test2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * @author fengke
 */
public class Draw extends JFrame{

    public static void main(String[] args){
        Draw dr = new Draw();
        dr.showdraw();
    }

    public void showdraw(){
        //继承后JFrame后无需实例化
        this.setTitle("绘图");
        this.setSize(700,500);
        //frame.setResizable(false);
        //
        this.setDefaultCloseOperation(3);
        this.setLocationRelativeTo(null);
        //排版
        FlowLayout f1 = new FlowLayout();
        this.setLayout(f1);
        //实例化监听器
        DrawListener lis = new DrawListener();
        //创建图形按钮和颜色按钮
        String[] array ={"Line","Rect","Oval","RoundRect","fillRect",
                "fillArc","fillOval","Triangle","Polygon"};
        for(int i=0 ; i<array.length ;i++){
            JButton button = new JButton(array[i]);
            this.add(button);
            button.addActionListener(lis);
        }
        Color[] colorArray = new Color[]{Color.black,Color.blue,
                Color.green,Color.red};
        for(int i=0 ; i<colorArray.length ; i++) {
            JButton button = new JButton();
            //设置文字好像又会影响基础的颜色显示功能
//            if (i == 0) {
//                button.setText("黑");
//            } else if (i == 1) {
//                button.setText("蓝");
//            } else if (i == 2) {
//                button.setText("绿");
//            } else if (i == 3) {
//                button.setText("红");
//            }
            //设置背景颜色（没反应）
            this.add(button);
            button.setBackground(colorArray[i]);

            //设置按钮大小
            button.setPreferredSize(new Dimension(30,30));
            //设置前景色，这个在正常工作，改变了前面字体的颜色
            button.setForeground(colorArray[i]);


            button.addActionListener(lis);
        }
        //setVisible设置窗体可见，放在所有组件后
        this.setVisible(true);

        Graphics g = this.getGraphics();
        //用this指代frame可能出错
        this.addMouseListener(lis);
        this.addMouseMotionListener(lis);

        lis.setG(g);

    }
}
